package com.area.cth.controller;

import com.area.cth.common.Result;
import com.area.cth.pojo.Thumb;
import com.area.cth.service.ThumbService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "点赞相关接口")
@RestController
@RequestMapping("/thumb")
@CrossOrigin
public class ThumbController {
    @Autowired
    private ThumbService ts;

    @Operation(summary = "新增点赞")
    @PostMapping
    public Result addLike(Integer pId) {
        Thumb thumb = ts.likeIf(pId);
        if (thumb == null) {
            ts.addLike(pId);
            return new Result(0,"点赞成功",null);
        } else {
            return Result.error("已点赞！请勿频繁操作！");
        }
    }

    @Operation(summary = "根据帖子id查询个人点赞")
    @GetMapping
    public Result<Thumb> likeIf(Integer pId) {
        Thumb b = ts.likeIf(pId);
        return Result.success(b);
    }

    @Operation(summary = "取消点赞")
    @DeleteMapping
    public Result deleteLike(Integer lId) {
        ts.deleteLike(lId);
        return new Result(0,"已取消",null);
    }

    @Operation(summary = "根据帖子id查询点赞数量")
    @GetMapping("/thumbCount")
    public Result<Integer> thumbCount(Integer pId) {
        Integer count = ts.thumbCount(pId);
        return Result.success(count);
    }
}
